package flc.ast.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.ve.EditorHolder;
import com.stark.ve.core.IEditorListener;

import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.adapter.AudioFormatAdapter;
import flc.ast.databinding.ActivityAudioExtractBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.media.audio.AudioFormat;
import stark.common.basic.media.audio.AudioPlayerImpl;
import stark.common.basic.media.audio.IAudioPlayer;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.TimeUtil;

public class AudioExtractActivity extends BaseAc<ActivityAudioExtractBinding> implements IAudioPlayer.IListener {

    public static String videoPath;
    private String audioPath;
    protected IAudioPlayer mAudioPlayer;


    @Override
    protected int onCreate() {
        return R.layout.activity_audio_extract;
    }

    @Override
    protected void initView() {
        mDataBinding.ivAudioExtractBack.setOnClickListener(v -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.ivPlay.setOnClickListener(this);
        mDataBinding.tvAudioExtractSave.setOnClickListener(this);

        mAudioPlayer = new AudioPlayerImpl();
        mAudioPlayer.setListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.ivPlay) {//播放
            if (mAudioPlayer == null) {
                return;
            }
            if (mAudioPlayer.isPlaying()) {
                mAudioPlayer.pause();
            } else {
                mAudioPlayer.resume();
            }
        } else if (id == R.id.tvAudioExtractSave) {//保存
            saveAudio();
        }
    }

    @Override
    protected void initData() {
        audioExt();
    }

    private void audioExt() {
        List<AudioFormat> audioFormats = EditorHolder.editor().supportAudioFormat();
        EditorHolder.editor().videoExtractAudio(videoPath, audioFormats.get(0), new IEditorListener() {
            @Override
            public void onSuccess(String outFilePath) {
                dismissDialog();
                ToastUtils.showShort(getString(R.string.extract_suc));
                audioPath = outFilePath;
                mAudioPlayer.play(audioPath);
            }

            @Override
            public void onFailure(String errMsg) {
                dismissDialog();
                ToastUtils.showShort(R.string.extract_def);
            }

            @Override
            public void onProgress(int progress) {
                showDialog(getString(R.string.extract_ing) + progress + getString(R.string.unit_percent));
            }
        });
    }

    //保存音频
    private void saveAudio() {
        showDialog(getString(com.stark.common.res.R.string.saving));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        FileP2pUtil.copyPrivateAudioToPublic(mContext, audioPath);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(com.stark.common.res.R.string.save_success);
                        startActivity(HomeActivity.class);
                    }
                });
            }
        }, 1000);
    }

    @Override
    public void onUpdatePlayTime(int curPlayTime, int duration) {
        mDataBinding.sbPlay.setMax((int) duration / 1000);
        mDataBinding.sbPlay.setProgress(curPlayTime / 1000);
        mDataBinding.tvPlayRight.setText(TimeUtils.millis2String(duration, "mm:ss"));
    }

    @Override
    public void onPlayChange(boolean isPlaying) {
        if (isPlaying) {
            mDataBinding.ivPlay.setImageResource(R.drawable.abf2);
        } else {
            mDataBinding.ivPlay.setImageResource(R.drawable.azt);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mAudioPlayer != null) {
            mAudioPlayer.resume();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mAudioPlayer != null) {
            mAudioPlayer.pause();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mAudioPlayer != null) {
            mAudioPlayer.stop();
        }
        FileUtils.deleteAllInDir(PathUtils.getFilesPathExternalFirst() + Content.myTemp);
    }
}